<?php
namespace App\Admin\Controllers\System;
use App\Admin\Controllers\System\Traits\androidPrivacyJsonTrait;
use App\Admin\Controllers\System\Traits\customTabbarTrait;
use App\Admin\Controllers\System\Traits\customTabbarTrait2;
use App\Admin\Controllers\System\Traits\desiaiPcClientTrait;
use App\Admin\Controllers\System\Traits\manifestJsonTrait;
use App\Admin\Controllers\System\Traits\pagesJsonTrait;
use App\Admin\Controllers\System\Traits\uniScssTrait;
use App\Models\WxTenant;
use App\Wen\Utils\Settings;
use App\Wen\Utils\StrUtils;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use ZipArchive;
use Illuminate\Support\Facades\File;

class AdminFrontController extends AdminController{

    use pagesJsonTrait,androidPrivacyJsonTrait,manifestJsonTrait,uniScssTrait,customTabbarTrait,customTabbarTrait2,desiaiPcClientTrait;

    protected $domain_auth_sign = '';
    protected $main_color = '#fc3a74';
    protected $domain = '';
    protected $uni_tabbar_list = [];
    protected $coins_name = '硬币';
    protected $page_index_path = '/pages/tabbar/index/index';
    protected $front_replace_list = [];



    public function auth_check($request){

        global $__MINI_GLOBAL_PAYCODE_0__;
        if(!$__MINI_GLOBAL_PAYCODE_0__){
            return false;
        }
        $this->domain_auth_sign = 1;
        return true;
        $res = Http::post('https://mini.minisns.cn/api/v1/three/domain/auth', [
            'domain' => str_replace(['https://', 'http://'], '', env('APP_URL', ''))
        ]);
        if($res->successful()){
            $res_ = $res->json();
            if($res_ && $res_['code']){
                if($res_['status']){
                    $this->domain_auth_sign = $res_['data'];
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

    public function init_var(){
        $this->main_color = strtolower(Settings::get('uni_color_primary', '#FC3A72'));
        $this->domain = str_replace(['https://', 'http://'], '', env('APP_URL', ''));
        $this->uni_tabbar_list = explode(',', Settings::get('uni_tabbar_list', '1,2,3,4,5'));
        $this->coins_name = Settings::get('app_coin_name', '硬币');
        $this->front_replace_list = Settings::get('uni_front_replace_list', []);

        if(_empty_($this->uni_tabbar_list)){
            exit('tabbar未配置');
        }
        if($this->uni_tabbar_list && $this->uni_tabbar_list[0] != 1){
            if($this->uni_tabbar_list[0] == 2){
                $this->page_index_path = '/pages/tabbar/shop/shop';
            }else if($this->uni_tabbar_list[0] == 4){
                $this->page_index_path = '/pages/tabbar/notice/notice';
            }else if($this->uni_tabbar_list[0] == 5){
                $this->page_index_path = '/pages/tabbar/mine/mine';
            }else if($this->uni_tabbar_list[0] == 19){
                $this->page_index_path = '/pages/tabbar/circle/circle';
            }else if($this->uni_tabbar_list[0] == 50001){
                $this->page_index_path = '/pages/tabbar/adopt/adopt';
            }else if($this->uni_tabbar_list[0] == 165){
                $this->page_index_path = '/pages/tabbar/city/city';
            }else if($this->uni_tabbar_list[0] == 157){
                $this->page_index_path = '/pages/tabbar/bureau/bureau';
            }else if($this->uni_tabbar_list[0] == 130){
                $this->page_index_path = '/pages/tabbar/voter/voter';
            }else if($this->uni_tabbar_list[0] == 142){
                $this->page_index_path = '/pages/tabbar/icons/icons';
            }else if($this->uni_tabbar_list[0] == 139){
                $this->page_index_path = '/pages/tabbar/exhibit/exhibit';
            }else if($this->uni_tabbar_list[0] == 131){
                $this->page_index_path = '/pages/tabbar/schedule/schedule';
            }else if($this->uni_tabbar_list[0] == 100){
                $this->page_index_path = '/pages/tabbar/used/used';
            }else if($this->uni_tabbar_list[0] == 50){
                $this->page_index_path = '/pages/tabbar/circleClass/circleClass';
            }else if($this->uni_tabbar_list[0] == 87){
                $this->page_index_path = '/pages/tabbar/category/category';
            }else if($this->uni_tabbar_list[0] == 97){
                $this->page_index_path = '/pages/tabbar/add/add';
            }
        }
    }



    public function uniapp(Request $request)
    {
        if(!file_exists(base_path('front')) || !file_exists(base_path('front/pages'))){
            return JsonResponse::make()
                ->error('请重新执行更新脚本文件，并等待脚本执行完成')
                ->send();
        }

        $flag = $this->auth_check($request);
        if(!$flag || _empty_($this->domain_auth_sign)){
            exit('<h2>'.str_replace(['https://', 'http://'], '', env('APP_URL', '')).'<span style="color: red;">未授权</span>'.'</h2>');
        }
        self::init_var();

        return $this->main_();
    }


    public function main_(){
        $folderPath = base_path('front'); // 你想要打包的文件夹的路径
        // 创建一个临时文件路径用于存储生成的Zip文件
        $zipFileName = 'minisns_front_'.get_mini_version_code().'.zip';
        $zipFilePath = storage_path('app/' . $zipFileName);

        // 创建一个ZipArchive对象并打开临时文件供写入
        $zip = new ZipArchive;
        $zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE);

        // 获取文件夹中的所有文件和目录
        $files = File::allFiles($folderPath);
        // 将文件夹中的每个文件添加到Zip文件中
        foreach ($files as $file) {
            $file_name = $file->getFilename();
            $local_name = $file->getRelativePath().'/'.$file_name;

            if(!$this->filter($local_name)){
                continue;
            }
            $content = File::get($file);

            $this->replaceProcess($content, $file_name, $local_name);
            $this->additionProcee($content, $file_name, $local_name);

            $zip->addFromString($local_name, $content);
            if(strpos($local_name, 'pages/tabbar/') !== false){
                $zip->addFromString(str_replace('/tabbar/', '/tabbar_clone/', $local_name), $content);
            }
        }
        // 加入生成文件
        $this->inser_tabbar_about($zip);
        $zip->addFromString('/pages.json', $this->get_pages_json_conten());
        $zip->addFromString('/androidPrivacy.json', $this->get_android_privacy_json_content());
        $zip->addFromString('/manifest.json', $this->get_manifest_json_content());
        $zip->addFromString('/uni.scss', $this->get_uni_scss_trait_content());

        // 添加图标
        if(file_exists(public_path('storage/logos/'))){
            $icon_files = File::allFiles(public_path('storage/logos/'));
            foreach ($icon_files as $icon) {
                $icon_name = $icon->getFilename();
                $zip->addFromString('unpackage/res/icons/'.$icon_name, File::get($icon));
            }
        }

        if(file_exists(public_path('storage/splashscreen/storyboard.zip'))){
            $zip->addFromString('unpackage/res/splashscreen/storyboard.zip', File::get(public_path('storage/splashscreen/storyboard.zip')));
        }

        if(file_exists(public_path('storage/splashscreen/480_762.9.png'))){
            foreach (['480_762.9.png', '720_1242.9.png', '1080_1882.9.png'] as $img_9_png){
                $zip->addFromString('unpackage/res/splashscreen/'.$img_9_png, File::get(public_path('storage/splashscreen/'.$img_9_png)));
            }
        }

        // 关闭ZipArchive对象
        $zip->close();

        // 记录所下载的版本号
        $download_front_versions = get_site_meta('my_download_front_versions', 'j');
        $mini_version = get_mini_version_code();
        if(!in_array($mini_version, $download_front_versions)){
            $download_front_versions[] = $mini_version;
            $download_front_versions = array_reverse($download_front_versions);
            if (count($download_front_versions) > 5) {
                // 只保留前5个元素
                $download_front_versions = array_slice($download_front_versions, 0, 10);
            }
            update_site_meta('my_download_front_versions', $download_front_versions, 'j');
        }


        // 将生成的Zip文件返回供下载
        return response()->download($zipFilePath, $zipFileName)->deleteFileAfterSend();
    }

    public function filter($local_name){
        if(in_array($local_name, ['/androidPrivacy.json', '/manifest.json', '/pages.json', '/uni.scss', 'static/tabbar/fabu.png',
            'static/tabbar/1.png', 'static/tabbar/2.png', 'static/tabbar/3.png', 'static/tabbar/4.png', 'static/tabbar/5.png',
            'static/tabbar/11.png', 'static/tabbar/22.png', 'static/tabbar/33.png', 'static/tabbar/44.png', 'static/tabbar/55.png',
            ])){
            return false;
        }

        if(strpos($local_name, 'pages/tabbar_clone/') !== false){
            return false;
        }

        return true;
    }

    private function inser_tabbar_about($zip){
        try {
            $zip->addFromString('static/tabbar/fabu.png', file_get_contents(public_path('storage/tarbar/fabu.png')));
            if(Settings::get('uni_tabbar_style', 1) == 2){
                $this->insert_mp_tabbar2($zip);
                $zip->addFromString('static/tabbar/1.png', file_get_contents(public_path('storage/tarbar/1.png')));
                $zip->addFromString('static/tabbar/2.png', file_get_contents(public_path('storage/tarbar/2.png')));
                $zip->addFromString('static/tabbar/3.png', file_get_contents(public_path('storage/tarbar/3.png')));
                $zip->addFromString('static/tabbar/4.png', file_get_contents(public_path('storage/tarbar/4.png')));
                $zip->addFromString('static/tabbar/5.png', file_get_contents(public_path('storage/tarbar/5.png')));

                $zip->addFromString('static/tabbar/11.png', file_get_contents(public_path('storage/tarbar/11.png')));
                $zip->addFromString('static/tabbar/22.png', file_get_contents(public_path('storage/tarbar/22.png')));
                $zip->addFromString('static/tabbar/33.png', file_get_contents(public_path('storage/tarbar/33.png')));
                $zip->addFromString('static/tabbar/44.png', file_get_contents(public_path('storage/tarbar/44.png')));
                $zip->addFromString('static/tabbar/55.png', file_get_contents(public_path('storage/tarbar/55.png')));

            }else{
                $this->insert_mp_tabbar($zip);
            }
        }catch (\Exception $e){
            throw new \Exception('请在[头像右上角-前端]重新提交保存试一下');
        }
    }

    public function additionProcee(&$content, $file_name, $local_name){
        if($local_name == '/app.scss'){
            $content .= Settings::get('app_custom_css_app_scss', '');
        }else if($local_name == 'pages/template/circle.scss'){
            $content .= Settings::get('app_custom_css_circle_scss', '');
        }
    }

    public function replaceProcess(&$content, $file_name, $local_name){
        if($local_name == 'config/api.js'){
            // 1、替换域名
            $content = str_replace([
                'mini.minisns.cn',
                'const safeCode = \'\'',
                'debug: true,'
            ], [
                $this->domain,
                'const safeCode = \''.env('APP_SAFE_CODE').'\'',
                'debug: false,'
            ], $content);
            $content = preg_replace("/version: '(\d+\.\d+\.\d+\.\d+|\d+\.\d+\.\d+|\d+\.\d+|\d+)'/", "version: '".get_mini_version_code()."'", $content);
        }else if($local_name == '/androidPrivacy.json'){
            // 1、替换域名
            $content = str_replace([
                'mini.minisns.cn',
                '#fc3a72'
            ], [
                $this->domain,
                $this->main_color
            ], $content);
        }

        if($this->main_color != '#fc3a72'){
            // 主题色替换
            if(in_array($local_name, ['/uni.scss', 'custom-tab-bar/index.js', '/pages.json', 'store/config/state.js', 'utils/util.js', 'common/wen.js'])){
                $content = str_replace('#fc3a72', $this->main_color, $content);
            }
        }

        if($local_name == 'common/wen.js'){
            $content = str_replace('/pages/tabbar/index/index', $this->page_index_path, $content);
            $content = str_replace('[1,19,4,5].includes', '['.implode(',', $this->uni_tabbar_list).'].includes', $content);
        } else if($local_name == 'store/state.js'){
            $content = str_replace('tabbar_style: 1', 'tabbar_style: '.Settings::get('uni_tabbar_style', 1), $content);
            if(Settings::get('is_multi_site', 0) > 0){

            }else{
                $tenant_obj = WxTenant::where('tenant_id', 0)->first();
                if($tenant_obj){
                    $content = str_replace("{tenant_id: null, name: '', short: '', logo: ''}","{tenant_id: null, name: '".$tenant_obj->name."', short: '".$tenant_obj->short."', logo: '".$tenant_obj->logo."'}", $content);
                }
            }
        }else if($local_name == 'pages/tabbar/index/index.vue'){
            if(in_array(1, $this->uni_tabbar_list)){
                $index = array_search(1, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 0})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 0)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 0)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 0)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }
                }
            }

            if(Settings::get('app_index_top_nav_style', 0) != 1){
                $content = str_replace('channels ?? {"style": 1}', 'channels ?? {"style": 0}', $content);
            }

            if(Settings::get('app_index_realtime_post', 0) == 1){
                $content = str_replace('//script(<<<JS<<<002<<<JS);', 'uni.wen.util.request(uni.wen.api.ApiRootUrl + \'realtime/top/posts\').then(function (res) {
			  if (res.status) {
			    that.setData({
			      real_time_posts: res.data.posts,
			      real_time_posts_time: res.data.format_time
			    });
			  }
			});', $content);

                $content = str_replace('<!-- template(<<<vue<<<000<<<vue); -->', '<block v-if="_type == 1 && real_time_posts.length > 0">
				<view class="post-list-gap"></view>
				<view class="top_ten_posts">
				  <view class="inner">
					  <view class="stitle">
					    <view class="left" @tap.stop.prevent="toSliderUrl(6, \'/pagesC/realtime/post\')">
					      <view class="logo_">
					        <image class="_image" :src="global__tenant__.logo"></image>
					      </view>
					      <view class="title_">{{ global__tenant__.name }}热门</view>
					      <view class="ago">{{ real_time_posts_time }}更新</view>
					    </view>
					    <view class="right" @tap.stop.prevent="toSliderUrl(6, \'/pages/common/search\')">
							<!-- #ifdef MP -->
							<i class="mini-icon mini-sousuo1"></i>
							<!-- #endif -->
						</view>
					  </view>
					  <scroll-view :scroll-y="true" class="apple-swiper-slidedown">
						    <swiper class="_index_real_swiper" :autoplay="false" :circular="false" interval="5000" :indicator-dots="true">
						      <swiper-item v-for="index in [1,2,3]" :key="index" v-if="index == 2 ? real_time_posts.length > 3 : (index == 3 ? real_time_posts.length > 6 : true)">
						        <view class="content">
						          <view class="item flex-row" v-for="subIndex in [1,2,3]" v-if="real_time_posts[(index - 1) * 3 + subIndex - 1]" :key="subIndex" @tap.stop.prevent="toSliderUrl(1, real_time_posts[(index - 1) * 3 + subIndex - 1].id)">
						            <view :class="\'order order_\' + ((index - 1) * 3 + subIndex)">{{ (index - 1) * 3 + subIndex }}</view>
						            <view class="post">{{ real_time_posts[(index - 1) * 3 + subIndex - 1].posts_content }}</view>
						            <block>
						              <view class="icon new" v-if="real_time_posts[(index - 1) * 3 + subIndex - 1].is_new">新</view>
						              <view class="icon hot" v-if="real_time_posts[(index - 1) * 3 + subIndex - 1].is_hot">热</view>
						            </block>
						          </view>
						        </view>
						      </swiper-item>
						    </swiper>
					  </scroll-view>
				  </view>
				</view>
			</block>', $content);

                $content = str_replace('<!-- template(<<<vue<<<001<<<vue); -->', Settings::get('app_custom_page_index_position_1', ''), $content);
            }

        }else if($local_name == 'pages/tabbar/shop/shop.vue'){
            if(in_array(2, $this->uni_tabbar_list)){
                $index = array_search(2, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/category/category.vue'){
            if(in_array(87, $this->uni_tabbar_list)){
                $index = array_search(87, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/circle/circle.vue'){
            if(in_array(19, $this->uni_tabbar_list)){
                $index = array_search(19, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/add/add.vue'){
            if(in_array(97, $this->uni_tabbar_list)){
                $index = array_search(97, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/adopt/adopt.vue'){
            if(in_array(50001, $this->uni_tabbar_list)){
                $index = array_search(50001, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/city/city.vue'){
            if(in_array(165, $this->uni_tabbar_list)){
                $index = array_search(165, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/bureau/bureau.vue'){
            if(in_array(157, $this->uni_tabbar_list)){
                $index = array_search(157, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/voter/voter.vue'){
            if(in_array(130, $this->uni_tabbar_list)){
                $index = array_search(130, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/icons/icons.vue'){
            if(in_array(142, $this->uni_tabbar_list)){
                $index = array_search(142, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/exhibit/exhibit.vue'){
            if(in_array(139, $this->uni_tabbar_list)){
                $index = array_search(139, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/schedule/schedule.vue'){
            if(in_array(131, $this->uni_tabbar_list)){
                $index = array_search(131, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/used/used.vue'){
            if(in_array(100, $this->uni_tabbar_list)){
                $index = array_search(100, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/circleClass/circleClass.vue'){
            if(in_array(50, $this->uni_tabbar_list)){
                $index = array_search(50, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 1})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 1)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }

                }
            }
        }else if($local_name == 'pages/tabbar/notice/notice.vue'){
            if(in_array(4, $this->uni_tabbar_list)){
                $index = array_search(4, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 3})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 2)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 2)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 2)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }
                }
            }
        }else if($local_name == 'pages/tabbar/mine/mine.vue'){
            if(in_array(5, $this->uni_tabbar_list)){
                $index = array_search(5, $this->uni_tabbar_list);
                $index_3 = array_search(3, $this->uni_tabbar_list);
                if($index !== false){
                    $content = str_replace('that.tabBarSetData({selected: 4})', 'that.tabBarSetData({selected: '.$index.'});', $content);
                    if($index_3 === false){
                        $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 3)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                    }else{
                        if($index > $index_3){
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 3)', 'that.$store.commit(\'updateTabbarIndex\', '.($index - 1).');', $content);
                        }else{
                            $content = str_replace('that.$store.commit(\'updateTabbarIndex\', 3)', 'that.$store.commit(\'updateTabbarIndex\', '.$index.');', $content);
                        }
                    }
                }
            }
        }else if($local_name == 'store/config/state.js'){
            if(Settings::get('app_index_top_nav_style', 0) != 1){
                $content = str_replace('"channels": {"style": 1},', '"channels": {"style": 0},', $content);
            }
            $content = str_replace('https://img.mini.minisns.cn/icons/abp.png', Settings::get('img_empty', 'https://img.mini.minisns.cn/icons/abp.png'), $content);
            $content = str_replace('wx863ab047d1981df9', Settings::get('app_id', ''), $content);
            $content = str_replace('gh_c4f1e13df531', Settings::get('app_meta_appid', ''), $content);
        }else if($local_name == 'store/mutations.js'){
            $index = array_search(4, $this->uni_tabbar_list);
            if($index === false){
                $index = -1;
            }
            $pub_index = array_search(3, $this->uni_tabbar_list);
            if($pub_index){
                if($pub_index < $index){
                    $index -= 1;
                }
            }
            $content = str_replace('let notice_index = 2;', 'let notice_index = '.($index < 0 ? 99 : $index).';', $content);
        }else if($local_name == 'contrib/components/common/toast/toast.vue'){
            if(Settings::get('uni_scss_loading_style', 0) == 1 ){
                // 抖音
                $content = str_replace('$global-loading-dy-style: none', '$global-loading-dy-style: block', $content);
                $content = str_replace('$global-loading-xhs-style: flex', '$global-loading-xhs-style: none', $content);
            }
        }else if($local_name == 'contrib/components/common/scroll-nav/scroll-nav.vue'){
            $content = str_replace('$page-index-box-menu-width: 70rpx', '$page-index-box-menu-width: '.Settings::get('page_index_box_menu_width', 70).'rpx', $content);
            $content = str_replace('$page-index-box-menu-wrap-width: 100rpx', '$page-index-box-menu-wrap-width: '.Settings::get('page_index_box_menu_wrap_width', 100).'rpx', $content);
            $content = str_replace('$page-index-box-menu-font-size: 26rpx', '$page-index-box-menu-font-size: '.Settings::get('page_index_box_menu_font_size', 26).'rpx', $content);
            $content = str_replace('$page-index-box-menu-border-radius: 50%', '$page-index-box-menu-border-radius: '.Settings::get('page_index_box_menu_border_radius', 50).'%', $content);
        }else if($local_name == 'contrib/components/common/hover-ball/hover-ball.vue'){
            if(Settings::get('uni_scss_global_hall_ball', 1) != 1){
                $content = str_replace('$global-hover-ball-display: block', '$global-hover-ball-display: none', $content);
            }
        }else if($local_name == 'components/common/water/product/index.scss'){
            $content = str_replace('$waterfall-img-max-height: 600rpx', '$waterfall-img-max-height: '.Settings::get('waterfall_img_max_height', 600).'rpx', $content);
            $content = str_replace('$waterfall-random-bg-font-size: 30rpx', '$waterfall-random-bg-font-size: '.Settings::get('waterfall_random_bg_font_size', 30).'rpx', $content);
            $content = str_replace('$waterfall-random-bg-padding: 20rpx', '$waterfall-random-bg-padding: '.Settings::get('waterfall_random_bg_padding', 20).'rpx', $content);
            $content = str_replace('$waterfall-random-bg-max-height: 120rpx', '$waterfall-random-bg-max-height: '.Settings::get('waterfall_random_bg_max_height', 120).'rpx', $content);
            $content = str_replace('$waterfall-random-bg-justify-content: start', '$waterfall-random-bg-justify-content: '.Settings::get('waterfall_random_bg_justify_content', 'start'), $content);

        }else if($local_name == 'pages/tabbar/index/index.scss'){
            $content = str_replace('$page-index-notice-box-height: 65rpx', '$page-index-notice-box-height: '.Settings::get('page_index_notice_box_height', 65).'rpx', $content);
            $content = str_replace('$page-index-notice-font-size: 25rpx', '$page-index-notice-font-size: '.Settings::get('page_index_notice_font_size', 25).'rpx', $content);
            $content = str_replace('$page-index-notice-img-height: 30rpx', '$page-index-notice-img-height: '.Settings::get('page_index_notice_img_height', 30).'rpx', $content);
            $content = str_replace('$page-index-topics-top-height: 400rpx', '$page-index-topics-top-height: '.Settings::get('page_index_topics_top_height', 400).'rpx', $content);
            if(Settings::get('page_publish_fixed_button', 0) == 1){
                $content = str_replace('$publish-fixed-icon-display: none', '$publish-fixed-icon-display: flex', $content);
            }
            $content = str_replace('/* css(<<<css<<<001<<<css); */', Settings::get('app_custom_page_index_scss', ''), $content);
        }else if($local_name == '/app.scss'){
            $content = str_replace('$page-refresh-fixed-button: none', '$page-refresh-fixed-button: '.(Settings::get('page_refresh_fixed_button', 0) == 1 ? 'flex' : 'none'), $content);
            $content = str_replace('$ellipsis-content-height: 150rpx', '$ellipsis-content-height: '.Settings::get('ellipsis_content_height', 150).'rpx', $content);
            if(Settings::get('uni_tabbar_style', 1) == 2){
                $content = str_replace('uni-tabbar .uni-tabbar__icon.uni-tabbar__icon__diff{width: unset!important;height: unset!important;}', '', $content);
            }
        }else if($local_name == 'pages/sticky/components/comment-scroll.vue'){
            $content = str_replace('$comment-list-sex-display: inline-block', '$comment-list-sex-display: '.(Settings::get('comment_list_sex_display', 0) == 1 ? 'none' : 'inline-block'), $content);
            if(Settings::get('comment_form_placeholder_text', '此时此刻想说~') != '此时此刻想说~'){
                $content = str_replace('此时此刻想说~', Settings::get('comment_form_placeholder_text', '此时此刻想说~'), $content);
            }
        }else if($local_name == 'pages/sticky/components/user-info.vue'){
            $content = str_replace('$post-list-sex-display: inline-block', '$post-list-sex-display: '.(Settings::get('post_list_sex_display', 0) == 1 ? 'none' : 'inline-block'), $content);
        }else if($local_name == 'pages/sticky/sticky.vue'){
            if(Settings::get('sticky_clrcle_display', 1) == 1){
                $content = str_replace('$sticky-clrcle-big-display: flex', '$sticky-clrcle-big-display: none', $content);
            }
        }else if($local_name == 'pages/sticky/components/post-content.vue'){
            if(Settings::get('sticky_clrcle_display', 1) == 1){
                $content = str_replace('$sticky-clrcle-mini-display: none', '$sticky-clrcle-mini-display: inline-flex', $content);
            }
        }else if($local_name == 'pages/sticky/components/comment-form.vue'){
            if(Settings::get('comment_form_placeholder_text', '此时此刻想说~') != '此时此刻想说~'){
                $content = str_replace('此时此刻想说~', Settings::get('comment_form_placeholder_text', '此时此刻想说~'), $content);
            }
        }else if($local_name == 'pages/sticky/components/post-footer.vue'){
            if(trim(Settings::get('post_list_footer_share_icon', '\e67e'), '\\') != 'e67e'){
                $content = str_replace('$post-list-footer-share-icon: "\e67e"', '$post-list-footer-share-icon: "\\'.trim(Settings::get('post_list_footer_share_icon', 'e67e'), '\\').'"', $content);
            }
            $content = str_replace('$post-list-footer-icon-size: 42rpx', '$post-list-footer-icon-size: '.Settings::get('post_list_footer_icon_font_size', 42).'rpx', $content);
        }else if($local_name == 'pages/common/tenants.vue'){
            if(Settings::get('multi_site_name', '学校') != '学校'){
                $content = str_replace('学校', Settings::get('multi_site_name', '学校'), $content);
            }
        }else if($local_name == 'mixins/base.js'){
            if(Settings::get('is_multi_site', 0) > 0){
                $content = str_replace('//script(<<<JS<<<001<<<JS);', file_get_contents(app_path('Admin/Controllers/System/assets/js/_001')), $content);
            }else{
                $tenant_obj = WxTenant::where('tenant_id', 0)->first();
                if($tenant_obj){
                    $content = str_replace("{tenant_id: null, name: '', short: '', logo: ''}","{tenant_id: null, name: '".$tenant_obj->name."', short: '".$tenant_obj->short."', logo: '".$tenant_obj->logo."'}", $content);
                }
            }
            $content = str_replace('#FC3A72', $this->main_color, $content);
            $content = str_replace('[1,19,4,5].includes', '['.implode(',', $this->uni_tabbar_list).'].includes', $content);

        }else if($local_name == 'mixins/forum.js'){
            if(Settings::get('comment_form_placeholder_text', '此时此刻想说~') != '此时此刻想说~'){
                $content = str_replace('此时此刻想说~', Settings::get('comment_form_placeholder_text', '此时此刻想说~'), $content);
            }
        }else if($local_name == 'utils/plusNative.js'){
            $content = str_replace('#FC3A72', $this->main_color, $content);
            $content = str_replace('#FEE2EA', Settings::get('uni_color_primary_lightest', '#fee2ea'), $content);
        }else if($local_name == 'utils/util.js'){
            if(Settings::get('multi_site_name', '学校') != '学校'){
                $content = str_replace('学校', Settings::get('multi_site_name', '学校'), $content);
            }
        }else if( $local_name == 'pages/common/login.vue'){
            if(Settings::get('default_user_name', '微信用户') != '微信用户'){
                $content = str_replace('微信用户', Settings::get('default_user_name', '微信用户'), $content);
            }
        }

        if(StrUtils::startsWith($local_name, 'pagesA/components/l-file')){
            if(Settings::get('app_upload_max_size_files', 10) != 10){
                $content = str_replace('maxSize = 10', 'maxSize = '.Settings::get('app_upload_max_size_files', '10'), $content);
//                $content = str_replace('maxSize: 10', 'maxSize: '.Settings::get('app_upload_max_size_files', '10'), $content);
            }
        }

        if($this->front_replace_list && count($this->front_replace_list) > 0){
            $content = str_replace(array_keys($this->front_replace_list), array_values($this->front_replace_list), $content);
        }

        if(env('circle_call', '圈子') != '圈子'){
            $content = str_replace('圈子', env('circle_call', '圈子'), $content);
        }

        if(env('circle_user_call', '圈主') != '圈主'){
            $content = str_replace('圈主', env('circle_user_call', '圈主'), $content);
        }

    }


    public function pagejson(){
        $page_json_str = $this->get_pages_json_conten();
        if (request()->isMethod('POST')) {
            return JsonResponse::make()->html($page_json_str)->success('搜索完毕');
        }else{
            $FileName = 'pages.json';
            $FilePath = storage_path('front/' . $FileName);
            if(!file_exists(storage_path('front'))){
                mkdir(storage_path('front'));
            }
            $fp = fopen($FilePath, "w");
            fwrite($fp, $page_json_str);
            fclose($fp);
            return response()->download($FilePath, $FileName)->deleteFileAfterSend();
        }
    }

    public function uniscss(){
        $file_str = $this->get_uni_scss_trait_content();
        if (request()->isMethod('POST')) {
            return JsonResponse::make()->html($file_str)->success('搜索完毕');
        }else{
            $FileName = 'uni.scss';
            $FilePath = storage_path('front/' . $FileName);
            if(!file_exists(storage_path('front'))){
                mkdir(storage_path('front'));
            }
            $fp = fopen($FilePath, "w");
            fwrite($fp, $file_str);
            fclose($fp);
            return response()->download($FilePath, $FileName)->deleteFileAfterSend();
        }
    }

    public function manifestjson(){
        $file_str = $this->get_manifest_json_content();
        if (request()->isMethod('POST')) {
            return JsonResponse::make()->html($file_str)->success('搜索完毕');
        }else{
            $FileName = 'manifest.json';
            $FilePath = storage_path('front/' . $FileName);
            if(!file_exists(storage_path('front'))){
                mkdir(storage_path('front'));
            }
            $fp = fopen($FilePath, "w");
            fwrite($fp, $file_str);
            fclose($fp);
            return response()->download($FilePath, $FileName)->deleteFileAfterSend();
        }
    }

}
